package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.FormalStudent;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("client")
public class ClientController {
    @Autowired
    public IClientService clientService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"client:view","潜在客户列表"},logical = Logical.OR)
    public String view() {
        return "client/client";
    }
    @RequestMapping("prospect")
    @RequiresPermissions(value = {"client:prospect","资源池列表"},logical = Logical.OR)
    public String prospect() {
        return "prospect/prospect";
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(ClientQueryObject qo) {
        PageResult pageResult = clientService.query(qo);
        return pageResult;
    }
    @ResponseBody
    @RequestMapping("query4Prospect")
    public PageResult query4Prospect(ClientQueryObject qo) {
        PageResult pageResult = clientService.query4Prospect(qo);
        return pageResult;
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Client> selectAll() {
        return clientService.selectAll();
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"client:delete","潜在客户删除"},logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            clientService.deleteByPrimaryKey(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("删除失败");
        }
    }
    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"client:saveOrUpdate","潜在客户新增或编辑"},logical = Logical.OR)
    public JSONResult saveOrUpdate(Client client){
        try {
            if(client.getId() != null){
                clientService.updateByPrimaryKey(client);
            }else{
                clientService.insert(client);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
    @ResponseBody
    @RequestMapping("follow")
    @RequiresPermissions(value = {"client:follow","资源池移交"},logical = Logical.OR)
    public JSONResult follow(Long id){
        try {
            clientService.follow(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
    @ResponseBody
    @RequestMapping("resourcePool")
    @RequiresPermissions(value = {"client:resourcePool","放入资源池"},logical = Logical.OR)
    public JSONResult resourcePool(Long clientId){
        try {
            clientService.resourcePool(clientId);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
    @ResponseBody
    @RequestMapping("validateName")
    @RequiresPermissions(value = {"client:resourcePool","放入资源池"},logical = Logical.OR)
    public Boolean validateName(String tel,String name){
            return !clientService.validateName(tel,name);
    }
    @ResponseBody
    @RequestMapping("positive")
    @RequiresPermissions(value = {"client:positive","转正"},logical = Logical.OR)
    public JSONResult positive(Long clientId, FormalStudent formalStudent){
        try {
            System.out.println(formalStudent);
            System.out.println(clientId);
            clientService.positive(clientId,formalStudent);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
}
